<?php

namespace app\models\product;

use app\models\constant\Status;
use Yii;

/**
 * This is the model class for table "product_area".
 *
 * @property int $id
 * @property string $name 名称
 * @property int $status 1 开启, 0关闭 -99删除
 * @property resource|null $func_order 下单规则
 * @property resource|null $func_pay 支付规则
 * @property resource|null $func_commission 返佣规则
 * @property resource|null $func_service 售后规则
 */
class ProductArea extends \yii\db\ActiveRecord
{
    use Status;

    public static $KEY = [
        'GROUP' => 5,

        'SCORE' => 2,

        // 报单, 复购, 秒杀, 商品区
        'REPORT' => 1,
        'REPEAT' => 6,
        'KILL' => 4,
        'GENERAL' => 3,

        // 远程是8
        'b84000' => 8,
    ];

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'product_area';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['status'], 'integer'],
            [['func_order', 'func_pay', 'func_commission', 'func_service'], 'string'],
            [['name'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => 'Name',
            'status' => 'Status',
            'func_order' => 'Func Order',
            'func_pay' => 'Func Pay',
            'func_commission' => 'Func Commission',
            'func_service' => 'Func Service',
        ];
    }

    // product area
    public static function getAreaId($product_id)
    {
        $area_id = ProductAreaList::find()
            ->where(['product_id' => $product_id])
            ->select(['area_id'])
            ->scalar();

        return $area_id;
    }

    // check repeat area
    public static function checkRepeat($product_id)
    {
        $area_id = self::getAreaId($product_id);

        return $area_id == self::$KEY['REPEAT'];
    }

    public static function checkReport($product_id)
    {
        $area_id = self::getAreaId($product_id);

        return $area_id == self::$KEY['REPORT'];
    }

    public static function checkB84000($product_id)
    {
        $area_id = self::getAreaId($product_id);

        return $area_id == self::$KEY['b84000'];
    }
}
